/**
 * Copyright (C) 2013 DaiKit.com - daikit4gxt-sample module (admin@daikit.com)
 *
 *         Project home : http://code.daikit.com/commons
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.daikit.daikit4gxtsample.client.ui.field;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.daikit.daikit4gxt.client.ui.cell.DkAsSimpleStringCell;
import com.daikit.daikit4gxt.client.ui.edit.CreateButtonSelectEvent;
import com.daikit.daikit4gxt.client.ui.edit.CreateButtonSelectEvent.CreateButtonSelectHandler;
import com.daikit.daikit4gxt.client.ui.edit.DeleteButtonSelectEvent;
import com.daikit.daikit4gxt.client.ui.edit.DeleteButtonSelectEvent.DeleteButtonSelectHandler;
import com.daikit.daikit4gxt.client.ui.edit.EditButtonSelectEvent;
import com.daikit.daikit4gxt.client.ui.edit.EditButtonSelectEvent.EditButtonSelectHandler;
import com.daikit.daikit4gxt.client.ui.forms.DkDualAsyncPagingEditorGrid;
import com.daikit.daikit4gxt.client.ui.forms.DkEditorGrid.Functionality;
import com.daikit.daikit4gxt.client.ui.popup.DkMessageBox;
import com.daikit.daikit4gxtsample.client.Main;
import com.daikit.daikit4gxtsample.client.propertyaccess.ShopPropertyAccess;
import com.daikit.daikit4gxtsample.client.screen.testscreen.producteditor.ShopEditCreatePopup;
import com.daikit.daikit4gxtsample.shared.bean.CityData;
import com.daikit.daikit4gxtsample.shared.bean.ShopData;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.sencha.gxt.data.shared.ListStore;
import com.sencha.gxt.data.shared.loader.PagingLoadConfig;
import com.sencha.gxt.data.shared.loader.PagingLoadResult;
import com.sencha.gxt.widget.core.client.grid.ColumnConfig;


/**
 * A dual grid for shop
 * 
 * @author tcaselli
 * @version $Revision$ Last modifier: $Author$ Last commit: $Date$
 */
public abstract class ShopDualGrid extends DkDualAsyncPagingEditorGrid<ShopData, ShopData>
{

	private static final Set<Functionality> leftFunctionalities = new HashSet<Functionality>();
	private static final Set<Functionality> rightFunctionalities = new HashSet<Functionality>();

	static
	{
		leftFunctionalities.add(Functionality.CREATE);
		leftFunctionalities.add(Functionality.EDIT);
		leftFunctionalities.add(Functionality.DELETE);

		rightFunctionalities.add(Functionality.EDIT);
	}

	/**
	 * Constructor
	 * 
	 * @param editorRightStore
	 */
	public ShopDualGrid(final ListStore<ShopData> editorRightStore)
	{
		super(editorRightStore, ShopPropertyAccess.INSTANCE, ShopPropertyAccess.INSTANCE, getColumnConfigs(), getColumnConfigs(),
				leftFunctionalities, rightFunctionalities, DualFunctionality.ALL);

		addLeftCreateSelectHandler(new CreateButtonSelectHandler()
		{
			@Override
			public void onSelect(final CreateButtonSelectEvent event)
			{
				ShopEditCreatePopup.get().showForCreation(getLeftStore());
			}
		});

		addLeftEditSelectHandler(new EditButtonSelectHandler<ShopData>()
		{
			@Override
			public void onSelect(final EditButtonSelectEvent<ShopData> event)
			{
				ShopEditCreatePopup.get().showForUpdate(getLeftStore(), event.getEditedObject());
			}
		});

		addLeftDeleteSelectHandler(new DeleteButtonSelectHandler<ShopData>()
		{
			@Override
			public void onSelect(final DeleteButtonSelectEvent<ShopData> event)
			{
				DkMessageBox.warn("TODO", "Functionality not implemented");
			}
		});

		addRightEditSelectHandler(new EditButtonSelectHandler<ShopData>()
		{
			@Override
			public void onSelect(final EditButtonSelectEvent<ShopData> event)
			{
				ShopEditCreatePopup.get().showForUpdate(getRightStore(), event.getEditedObject());

			}
		});

		getLeftEditorGrid().setHeadingHtml("HEADING");
		getRightEditorGrid().setHeadingHtml("HEADING");
	}

	private static final List<ColumnConfig<ShopData, ?>> getColumnConfigs()
	{
		final List<ColumnConfig<ShopData, ?>> columnConfigs = new ArrayList<ColumnConfig<ShopData, ?>>();
		final ColumnConfig<ShopData, String> columnId = new ColumnConfig<ShopData, String>(ShopPropertyAccess.INSTANCE.id(), 100,
				Main.gwtSampleI18n().shop_id());
		final ColumnConfig<ShopData, String> columnName = new ColumnConfig<ShopData, String>(ShopPropertyAccess.INSTANCE.name(),
				100, Main.gwtSampleI18n().shop_name());
		final ColumnConfig<ShopData, CityData> columnCity = new ColumnConfig<ShopData, CityData>(
				ShopPropertyAccess.INSTANCE.city(), 100, Main.gwtSampleI18n().shop_city());

		columnCity.setCell(new DkAsSimpleStringCell<CityData>());

		columnConfigs.add(columnId);
		columnConfigs.add(columnName);
		columnConfigs.add(columnCity);
		return columnConfigs;
	}

	@Override
	protected ShopData convertRightElement(final ShopData rightElement)
	{
		return rightElement;
	}

	@Override
	protected ShopData convertLeftElement(final ShopData leftElement)
	{
		return leftElement;
	}

	@Override
	protected void callLoadLeftRpc(final PagingLoadConfig pagingLoadConfig,
			final AsyncCallback<PagingLoadResult<ShopData>> callback)
	{
		Main.controller().rpcProduct().getShops(null, pagingLoadConfig, callback);
	}
}
